package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.impl.PartShadowPopupView;
import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.MemberChooseLeftItemBinding;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.CopyUtils;
import com.swgk.core.view.selection.GridItemDecoration;
import com.swgk.core.view.selection.XChooseChildAdapter;
import com.swgk.core.view.selection.XChooseEntity;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ViewConstructor")
public class TenderTypeOtherPopup extends PartShadowPopupView {

    private CommonPopEntity mCurBean;
    // 缓存数据集
    private final SparseArray<XChooseEntity> mData;
    // 勾选缓存
    private final SparseIntArray mFilters;
    private BaseRecycleAdapter<XChooseEntity> mMainAdapter;
    String[] cateTitle;
    private final IResult simpleResult;
    private MaxRecyclerView recyclerView;

    public TenderTypeOtherPopup(@NonNull Context context,
                                String[] cateTitle,
                                SparseArray<XChooseEntity> mData,
                                SparseIntArray mFilters,
                                IResult simpleResult) {
        super(context);
        this.cateTitle = cateTitle;
        this.simpleResult = simpleResult;
        this.mData = mData;
        this.mFilters = mFilters;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.member_choose_left_dialog;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView resetView = findViewById(R.id.reset_view);
        resetView.setText("取消");
        resetView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick()) {
                    dismiss();
                }
            }
        });
        findViewById(R.id.ok_view).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick()) {
                    dismiss();
                    simpleResult.onResult(mCurBean, mData, mFilters);
                }
            }
        });

        recyclerView = findViewById(R.id.type_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        initMainAdapter();
    }

    public void initMainAdapter() {
        List<XChooseEntity> entities = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            XChooseEntity datum = mData.get(key);
            if (datum.getList() == null || datum.getList().size() == 0) {
                continue;
            }
            entities.add(datum);
        }
        mMainAdapter = new BaseRecycleAdapter<XChooseEntity>(getContext(), R.layout.member_choose_left_item, entities) {
            @Override
            public void convert(ItemViewHolder holder, XChooseEntity o) {
                MemberChooseLeftItemBinding testBinding = (MemberChooseLeftItemBinding) holder.getmBinding();
                testBinding.tvCategory.setText(o.getTypeName());
                testBinding.arrowLayout.setVisibility(INVISIBLE);
                updateChildRecycler(testBinding.rv, o, mFilters.get(o.getSort()), true);
            }
        };
        recyclerView.setAdapter(mMainAdapter);
    }

    public void updateChildRecycler(RecyclerView recyclerView, XChooseEntity parentEntity, int defItem, boolean isExpand) {
        XChooseChildAdapter mAdapter = new XChooseChildAdapter(getContext(), parentEntity.getList(), R.layout.member_choose_left_child_item, isExpand);
        mAdapter.setItemSelect(true);
        if (recyclerView.getItemDecorationCount() > 0) {
            recyclerView.removeItemDecorationAt(0);
        }
        recyclerView.addItemDecoration(new GridItemDecoration(10));
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setDefItem(defItem);
        mAdapter.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                mFilters.put(parentEntity.getSort(), position);
                entity.setId(parentEntity.getSort() + ""); // 记录是几级筛选
                mCurBean = entity;
                // 清除大于当前分类的数据
                for (int i = 0; i < mData.size(); i++) {
                    int key = mData.keyAt(i);
                    XChooseEntity datum = mData.get(key);
                    if (datum.getList() == null || datum.getList().size() == 0) {
                        continue;
                    }
                    if (key > parentEntity.getSort()) {
                        mData.get(key).setList(new ArrayList<>());
                        mFilters.put(key, 0);
                    }
                }
                if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                    if (!entity.getChildren().get(0).getTypeName().equals("全部")) {
                        entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                    }
                    int sort = parentEntity.getSort() + 1;
                    if (cateTitle.length > sort) {
                        mData.put(sort, new XChooseEntity(sort, cateTitle[sort], entity.getChildren()));
                    } else {
                        mData.put(sort, new XChooseEntity(sort, "分类", entity.getChildren()));
                    }
                }
                //重组数据
                List<XChooseEntity> entities = new ArrayList<>();
                for (int i = 0; i < mData.size(); i++) {
                    int key = mData.keyAt(i);
                    XChooseEntity datum = mData.get(key);
                    if (datum.getList() == null || datum.getList().size() == 0) {
                        mFilters.put(datum.getSort(), 0);
                        continue;
                    }
                    entities.add(datum);
                }
                mMainAdapter.setDatas(entities);
            }
        });
    }

    public interface IResult {
        void onResult(CommonPopEntity entity, SparseArray<XChooseEntity> data, SparseIntArray filter);
    }
}
